package com.zhiwang.xiaoqu.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.zhiwang.xiaoqu.widget.glide.transformation.RoundedCornersTransformation

class LoopViewPagerAdapter : PagerAdapter {

    var context: Context
    var datas: ArrayList<String>


    val imageViewList = arrayListOf<ImageView>()


    val TAG = "LoopViewPagerAdapter"

    val requestOpts = RequestOptions().fitCenter()
        .transform(RoundedCornersTransformation(20, 5))

    constructor(context: Context, datas: ArrayList<String>) {

        this.context = context
        this.datas = datas

    }


    override fun instantiateItem(container: ViewGroup, position: Int): Any {

        val view = ImageView(context)

        view.scaleType = ImageView.ScaleType.FIT_XY
        view.layoutParams = ViewPager.LayoutParams()

        container.addView(view)

        Log.i(TAG, "instantiateItem: $position ${position % datas.size}")

        Glide.with(context)
            .load(datas[position % datas.size])
            .into(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`

    override fun getCount() = 1000
}